<template>
  <div>
    <div class="">
      <div class="pt16">
        <el-table size="mini" :data="list" border style="width: 100%;">
          <el-table-column label="最小值" width="180" align="center">
            <template slot-scope="scope">
              <el-input size="small" v-model="scope.row.min" type="number"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="不小于" width="60" align="center">
            <template slot-scope="scope">
              <span><=</span>
            </template>
          </el-table-column>
          <el-table-column label="费率" width="120" align="center">
            <template slot-scope="scope">
              <el-input size="small" type="number" v-model="scope.row.rate">
                <template slot="append">%</template>
              </el-input>
            </template>
          </el-table-column>
          <el-table-column label="小于" width="60" align="center">
            <template slot-scope="scope">
              <span><</span>
            </template>
          </el-table-column>
          <el-table-column label="最大值" width="180" align="center">
            <template slot-scope="scope">
              <el-input size="small" type="number" v-model="scope.row.max"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="180" align="center">
            <template slot-scope="scope">
              <div>
                <el-button v-if="tableData.length == (scope.$index+1)" size="mini" icon="el-icon-plus" @click="specsFunc(scope.$index, scope.row)">添加</el-button>
                <el-button v-else size="mini" icon="el-icon-minus" @click="deleteClick(scope.$index)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import { mergeTable } from '@/utils/table.js';
export default {
  components: {
  },
  data() {
    return {
      /*选择规格*/
      list: []
    };
  },
  props: {
    tableData: Array
  },
  created() {
    this.list = this.tableData;
  },
  watch:{
    tableData:{
      handler:function(newValue,oldValue){
        this.list = newValue;
      },
      deep:true
    },
    list:{
      handler:function(newValue,oldValue){
        console.log('deep',newValue);
         this.$emit('change',newValue );
      },
      deep:true
    }
  },
  methods: {
    /*打开规格*/
    specsFunc() {
      this.list.push(
        {
          min:0,
          rate:0,
          max:0
        }
      )
    },

    /*删除*/
    deleteClick(e, isType) {
      this.list.splice(e, 1);
      this.list = mergeTable(this.list);
    },

    /*表格合并行*/
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (row.rowSpan != null) {
          return {
            rowspan: row.rowSpan,
            colspan: 1
          };
        } else {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
    }
  }
};
</script>

<style lang="scss">
.spec-min-box > i {
  position: absolute;
  right: 0;
  font-size: 16px;
  cursor: pointer;
}
.spec-min-box .el-icon-close {
  top: 0;
  color: red;
  visibility: hidden;
}
td:hover .spec-min-box .el-icon-close {
  visibility: visible;
}
.spec-min-box .el-icon-caret-right {
  bottom: 0;
  transform: rotate(45deg);
  color: #3a8ee6;
}
.el-form-item.is-error .active-all-set .el-input__inner,
.el-form-item.is-error .isvalid .el-input__inner {
  border-color: #c0c4cc;
}
</style>
